package Dao;

import Entity.Officer;
import Utils.SimpleTools;
import javafx.scene.control.Alert;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SignupDao {

    //向Staff.json文件添加注册通过的用户
    public static void logup(String[] p) throws IOException {
        File file = new File("files\\Officer.json");
        ObjectMapper om = new ObjectMapper();
        List<Officer> list = new ArrayList<>();
        if(file.exists() && file.length() != 0){
            list = om.readValue(file, new TypeReference<List<Officer>>() {});
        }

        boolean i = true;
        Officer officer = new Officer(p[0],p[1],p[2],p[3],p[3].substring(6,14),p[4],p[5],p[6],1);
        for(Officer officerd:list){
            if(officerd.getUsername().equals(p[0])){
                SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "抱歉", "该用户名已存在");
                i = false;
            }
        }
        if(i){
            list.add(officer);
            om.writeValue(file,list);
        }
    }

    //判断职工里是否含有该用户名
    public static boolean iscontain(String username) throws IOException {
        File file = new File("files\\Officer.json");
        ObjectMapper om = new ObjectMapper();
        if(file.exists() && file.length() != 0){
            List<Officer> list = om.readValue(file, new TypeReference<List<Officer>>() {});

            for(Officer officer:list){
                if(officer.getUsername().equals(username)) return true;
            }
        }
        return false;
    }
}
